package com.zx.viewlearn.views.mypullrorefresh;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zx.viewlearn.R;

/**
 * Created by zhoux on 2018/2/11.
 * 注释：
 */

public class LoadMoreView extends RelativeLayout implements StateListener {
    class ViewHolder {
        TextView text_view;
        SuperLoadingProgress loading_iv;

        public ViewHolder(View root) {
            this.text_view = root.findViewById(R.id.load_text_view);
            this.loading_iv = root.findViewById(R.id.loading_iv);
//            Log.e("RefreshView", "ViewHolder");
        }

        public void setTextStr(String text) {
            if (text_view != null) this.text_view.setText(text);
        }

        public void startLoading() {
            if (loading_iv != null) {
                loading_iv.setProgress(0);
                while (loading_iv.getProgress() < 100) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    loading_iv.setProgress(loading_iv.getProgress() + 1);
                }
//                if (!loading_iv.isSuccess){
//                    startLoading();
//                }
            }
        }

        public void stopLoading() {
            if (loading_iv != null)
                loading_iv.finishSuccess();

        }

        public void init() {

        }
    }

    ViewHolder mViewHolder;


    public LoadMoreView(Context context) {
        super(context);
        init(context, null);
    }

    public LoadMoreView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
    }

    private void initViews() {
        mViewHolder = new ViewHolder(this);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        initViews();


    }

    @Override
    public void init() {
        mViewHolder.setTextStr("上拉加载更多");

    }

    @Override
    public void releaseToDo() {
        mViewHolder.setTextStr("释放后加载更多");
    }


    @Override
    public void done() {
        //停止动画
        mViewHolder.startLoading();
        mViewHolder.setTextStr("加载完成");

    }

    @Override
    public void doing() {
        //这边可以加载动画
        mViewHolder.stopLoading();
        mViewHolder.setTextStr("正在加载");

    }


}
